<?php 
class Contact extends Controller
{
	function __construct()
	{
		parent::__construct();
        //$this->output->enable_profiler(TRUE);
        $this->load->model(array('Directory_user_model'));
	}
	
	function index()
	{
		$data = array();
		$this->load->helper(array('form','url'));
		$this->load->library(array('form_validation','email','recaptcha'));
        
        $data['recaptcha']=$this->recaptcha->get_html();
		
		/*$data['your_name'] = 'your name';
		$data['your_company'] = 'your company name';
		$data['email'] = 'your e-mail';
		$data['telephone'] = 'your contact number';
		$data['message'] = 'your message';*/
		
		if(isset($_POST['submit_x']) || isset($_POST['submit']))
		{
			$this->form_validation->set_error_delimiters('<div class="error">','</div>');
			$this->form_validation->set_rules('recaptcha_response_field', 'Captcha field', 'callback_check_captcha');
            $this->form_validation->set_rules('name','name','required|trim|xss_clean');			
			$this->form_validation->set_rules('company','company','required|trim|xss_clean');			
			$this->form_validation->set_rules('telephone','telephone','required|trim|xss_clean');			
			$this->form_validation->set_rules('email','e-mail','required|trim|xss_clean|valid_email');			
			$this->form_validation->set_rules('message','message','required|trim|xss_clean');
			
			//$this->validation->set_fields($fields);
			
			$this->form_validation->set_message('required', 'Please enter %s');
			
			$this->form_validation->set_message('valid_email', 'Please enter valid %s address');
			
			if ($this->form_validation->run() == TRUE)
			{
 				$this->load->model('admin_contact_model');
				                
                $name = $this->input->post('name');
                $company = $this->input->post('company');
                $telephone = $this->input->post('telephone');
                $email = $this->input->post('email');
                $message = nl2br($this->input->post('message'));
                    
                $sends = $this->admin_contact_model->send_contact_email($name,$company,$telephone,$email,$message);
				//mail send to admin

				if($sends)
				{
					redirect('contact/success');	
				}
			}
			else
			{
				
				unset($data);
                $data['recaptcha']=$this->recaptcha->get_html();
				$data['your_name'] = $this->input->post('name');
				$data['your_company'] = $this->input->post('company');
				$data['email'] = $this->input->post('email');
				$data['telephone'] = $this->input->post('telephone');
				$data['message'] = $this->input->post('message');
			}
				
		}
		
		$this->load->view(TEMPLATE_NAME.'/front/contact_view', $data);
	}
	
    function check_captcha($val) {
        $result = $this->recaptcha->check_answer($_SERVER["REMOTE_ADDR"],$this->input->post('recaptcha_challenge_field'),$val);
        if ($result) {
            return true;
        } else {
            $this->form_validation->set_message('check_captcha','Incorrect Security Image Response');
            return false;
        }
    }
    
	function success()
	{
		$this->load->view(TEMPLATE_NAME.'/front/contact_success_view', $data);
	}
}
?>